﻿
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Threading;
namespace WCYCReader.ViewModels
{
    public class PropertyChangedHelper : INotifyPropertyChanged
    {
        private object containerObject;
        private static Dictionary<string, PropertyChangedEventArgs> eventArgsCache = new Dictionary<string, PropertyChangedEventArgs>();
        private PropertyChangedEventHandler propChangedEventHandler;
        private static object syncRoot = new object();

        public event PropertyChangedEventHandler PropertyChanged;

        public PropertyChangedHelper()
        {
        }

        public PropertyChangedHelper(object container)
        {
            this.containerObject = container;
        }

        private static PropertyChangedEventArgs GetPropertyChangedEventArgs(string propName)
        {
            PropertyChangedEventArgs args = null;
            if (!eventArgsCache.ContainsKey(propName))
            {
                lock (syncRoot)
                {
                    if (!eventArgsCache.ContainsKey(propName))
                    {
                        args = new PropertyChangedEventArgs(propName);
                        eventArgsCache.Add(propName, args);
                    }
                    return args;
                }
            }
            return eventArgsCache[propName];
        }

        public void SendPropertyChanged(params string[] propertyNames)
        {
            if (((this.propChangedEventHandler != null) && (propertyNames != null)) && (propertyNames.Length >= 0))
            {
                string propName = null;
                for (int i = 0; i < propertyNames.Length; i++)
                {
                    if (this.propChangedEventHandler != null)
                    {
                        try
                        {
                            propName = propertyNames[i];
                            this.propChangedEventHandler((this.containerObject != null) ? this.containerObject : this, GetPropertyChangedEventArgs(propName));
                        }
                        catch { }
                    }
                }
            }
        }

        public void SendPropertyChanged(string propertyName)
        {
            this.SendPropertyChanged(new string[] { propertyName });
        }

        [DebuggerStepThrough, Conditional("DEBUG")]
        private void VerifyPropertyName(string propName)
        {
            if (this.containerObject.GetType().GetProperty(propName) == null)
            {
                throw new ArgumentException("Property not found", propName);
            }
        }
    }
}

